const { Controller } = require('egg');
const validate = require('../rule/rule');

class RoleController extends Controller {

  /**
   * @param req
   * @param res
   * @api { GET } /userManage/role/list 获取角色列表
   * @apiName list
   * @apiGroup role
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiQuery { String } name 角色名称
   * @apiQuery { String } order 排序字段
   * @apiQuery { String } method 排序方式
   * @apiQuery { Number } pageIndex 页码
   * @apiQuery { Number } pageSize 页码容量
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async list(req, res) {
    const ctx = this.ctx;
    try {
      const { query } = req;
      const { name, order, method, pageIndex, pageSize } = validate.roleListValidate(query);
      const data = ctx.service.role.list(name, order, method, pageIndex, pageSize);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @param req
   * @param res
   * @api { POST } /userManage/role/insert 新增角色
   * @apiName insert
   * @apiGroup role
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } name 角色名称
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async insert(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { name } = validate.roleInsertValidate(body);
      const data = ctx.service.role.insert(name);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @param req
   * @param res
   * @api { POST } /userManage/role/update 更新角色
   * @apiName update
   * @apiGroup role
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } name 角色名称
   * @apiParam { String } uuid 角色id
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async update(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { name, uuid } = validate.roleUpdateValidate(body);
      const data = ctx.service.role.update(name, uuid);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   *
   * @param req
   * @param res
   * @api { POST } /userManage/role/delete 删除角色
   * @apiName delete
   * @apiGroup role
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { Object } ids
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async delete(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { ids } = validate.roleDeleteValidate(body);
      const data = await ctx.service.role.delete(ids);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  async init() {
    try {
      const data = await this.ctx.service.role.initRole();
      this.ctx.status = 200;
      this.ctx.sendSuccess({ data });
    } catch (error) {
      this.ctx.status = 200;
      this.ctx.sendFailed({ msg: error.message });
      return;
    }
  }


}

module.exports = RoleController;
